package com.atguigu.gulimall.search.thread;

import java.util.concurrent.*;

public class ThreadTest2 {
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("main....start");
        //runAsync没返回值的
        /*CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
        }, executorService);*/

        //supplyAsync有返回值
        /*CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 2;
            System.out.println("运行结果：" + i);
            return i;
        }, executorService);
        Integer integer = future.get();
        System.out.println("main....end。。。"+ integer);*/

        //方法完成后的感知
        /*CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 0;
            System.out.println("运行结果：" + i);
            return i;
        }, executorService).whenComplete((res,exception)->{
            //虽然能得到异常信息，但是没法修改返回数据。
            System.out.println("异步任务成功完成了..结果是："+res+"；异常是："+exception);
        }).exceptionally(throwable -> {
            //可以感知异常，同时返回默认值
            return 10;
        });*/

        //方法执行完成后的处理
        /*CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 4;
            System.out.println("运行结果：" + i);
            return i;
        }, executorService).handle((res,thr)->{
            if(res!=null){
                return res*2;
            }
            if(thr != null){
                return 0;
            }
            return 0;
        });*/

//        Integer integer = future.get();

        /**
         * 线程串行化
         *  1、thenRun：不能获取到上一步的执行结果
         *  .thenRunAsync(() -> {
         *             System.out.println("任务2启动了");
         *         }, executorService)
         *  2、thenAcceptAsync能接受上一步结果，但是无返回值
         */
        /*CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getId());
            int i = 10 / 4;
            System.out.println("运行结果：" + i);
            return i;
        }, executorService).thenApplyAsync((res) -> {
            System.out.println("任务2启动了..." + res);
            return "Hello " + res;
        }, executorService);
        System.out.println("main....end。。。"+future.get());
        */

        /**
         * 两个都完成
         */
        /*CompletableFuture<Object> future01 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1线程1：" + Thread.currentThread().getId());
            int i = 10 / 4;
            System.out.println("任务1结束：" + i);
            return i;
        }, executorService);

        CompletableFuture<Object> future02 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2线程2：" + Thread.currentThread().getId());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("任务2结束：" );
            return "Hello";
        }, executorService);*/
        /*future01.runAfterBothAsync(future02,()->{
                System.out.println("任务3开始：" );
        },executorService);*/
        /*future01.thenAcceptBothAsync(future02,(f1,f2)->{
            System.out.println("任务3开始：" +f1 + "-->" + f2);
        },executorService);*/

       /* CompletableFuture<String> future = future01.thenCombineAsync(future02, (f1, f2) -> {
            System.out.println("任务3开始：" + f1 + "-->" + f2);
            return f1 + ":" + f2 + " -> HaHa";
        }, executorService);*/

        /**
         * 两个任务只要有一个完成，我们就执行任务3
         * runAfterEitherAsync : 不感知结果，自己也无返回值
         *acceptEitherAsync : 感知结果，自己没有返回值
         * applyToEitherAsync  : 感知结果，自己有返回值
         */
        /*future01.runAfterEitherAsync(future02,()->{
            System.out.println("任务3开始：");
        },executorService);*/
       /* future01.acceptEitherAsync(future02,(res)->{
            System.out.println("任务3开始：" + res);
        },executorService);*/
        /*CompletableFuture<String> future = future01.applyToEitherAsync(future02, (res) -> {
            System.out.println("任务3开始：" + res);
            return res.toString() + " HAHA";
        }, executorService);
        System.out.println("main....end。。。"+future.get());*/

        CompletableFuture<String> futureImg = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的图片信息");
            return "hello.jpg";
        }, executorService);
        CompletableFuture<String> futureAttr = CompletableFuture.supplyAsync(() -> {
            System.out.println("查询商品的属性信息");
            return "黑色+256G";
        }, executorService);
        CompletableFuture<String> futureDesc = CompletableFuture.supplyAsync(() -> {
            try {
                Thread.sleep(3000);
                System.out.println("查询商品的介绍信息");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return "华为";
        }, executorService);

//        CompletableFuture<Void> allOf = CompletableFuture.allOf(futureImg, futureAttr, futureDesc);
        CompletableFuture<Object> anyOf = CompletableFuture.anyOf(futureImg, futureAttr, futureDesc);
//        allOf.get();//等待所有结果完成
        anyOf.get();//等待所有结果完成


//        System.out.println("main....end。。。" + futureImg.get() + "=>" + futureAttr.get() + "=>" +futureDesc.get());
        System.out.println("main....end。。。" + anyOf.get());
    }
}
